package com.quectel.business.common.thirdconfig;

import com.quectel.constant.core.thirdplatformconfig.ThirdPlatformConfigConstants;
import com.quectel.core.module.thirdconfig.dto.ThirdPlatformConfigDto;
import com.quectel.core.module.thirdconfig.dto.item.BaiduBCE;
import com.quectel.core.module.thirdconfig.dto.item.Sms;
import com.quectel.core.module.thirdconfig.dto.item.WechatOfficial;
import com.quectel.core.module.thirdconfig.service.ThirdPlatformConfigService;
import com.quectel.util.common.JacksonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * 第三方配置
 *
 * @author: caolu
 * @email: louis.cao@quectel.com
 * @date: 2022-05-09 12:52:00
 */
@Component
public class ThirdPlatformConfigBusiness {
    @DubboReference
    private ThirdPlatformConfigService thirdPlatformConfigService;

    /**
     * 模拟短信是否开启
     *
     * @param tenantId
     * @return
     */
    public boolean smsMockEnable(Long tenantId) {
        Sms smsConfig = getSmsConfig(tenantId);
        return smsConfig != null && Boolean.TRUE.equals(smsConfig.getMockEnable());
    }

    /**
     * 得到短息配置
     *
     * @param tenantId
     * @return
     */
    public Sms getSmsConfig(Long tenantId) {
        ThirdPlatformConfigDto thirdPlatformConfigDto = thirdPlatformConfigService.selectCacheByTenantAndType(tenantId, ThirdPlatformConfigConstants.PlatformType.SMS_CONFIG.getType());
        if (thirdPlatformConfigDto == null || StringUtils.isBlank(thirdPlatformConfigDto.getConfigJson())) {
            return null;
        }
        return JacksonUtils.parse(thirdPlatformConfigDto.getConfigJson(), Sms.class);
    }

    /**
     * 得到公众号相关配置
     *
     * @param tenantId
     * @return
     */
    public WechatOfficial getWechatOfficialConfig(Long tenantId) {
        ThirdPlatformConfigDto thirdPlatformConfigDto = thirdPlatformConfigService.selectCacheByTenantAndType(tenantId, ThirdPlatformConfigConstants.PlatformType.WECHAT_OFFICIAL_CONFIG.getType());
        if (thirdPlatformConfigDto == null || StringUtils.isBlank(thirdPlatformConfigDto.getConfigJson())) {
            return null;
        }
        return JacksonUtils.parse(thirdPlatformConfigDto.getConfigJson(), WechatOfficial.class);
    }

    /**
     * 得到公众号相关配置
     *
     * @param tenantId
     * @return
     */
    public BaiduBCE getBaiduBCEConfig(Long tenantId) {
        ThirdPlatformConfigDto thirdPlatformConfigDto = thirdPlatformConfigService.selectCacheByTenantAndType(tenantId, ThirdPlatformConfigConstants.PlatformType.BAIDU_BCE_CONFIG.getType());
        if (thirdPlatformConfigDto == null || StringUtils.isBlank(thirdPlatformConfigDto.getConfigJson())) {
            return null;
        }
        return JacksonUtils.parse(thirdPlatformConfigDto.getConfigJson(), BaiduBCE.class);
    }
}
